#!/bin/bash

echo "=== 编译 cbp2make_ai ==="

CXX=g++
CXXFLAGS="-I../cbp2make/lib -I../cbp2make/lib/tinyxml -I../cbp2make/src -Ilib -Isrc -Wall -std=c++11 -DAICODER_ENHANCED -m64 -D__WXMSW__ -DwxUSE_UNICODE"
LDFLAGS="-m64"
OBJDIR=".objs/wx30_64"
BINDIR="bin"
TARGET="$BINDIR/cbp2make_ai_wx30_64"

# 创建目录
mkdir -p $OBJDIR $BINDIR

# 编译函数
compile_file() {
    local src=$1
    local obj=$2
    echo "编译: $src"
    $CXX $CXXFLAGS -c "$src" -o "$obj"
    return $?
}

# 编译所有文件
echo "1. 编译 AI 增强文件..."
compile_file "src/aicoder_enhancements.cpp" "$OBJDIR/aicoder_enhancements.o" || exit 1

echo "2. 编译核心文件..."
for file in ../cbp2make/src/*.cpp; do
    if [ -f "$file" ]; then
        base=$(basename "$file" .cpp)
        compile_file "$file" "$OBJDIR/$base.o" || exit 1
    fi
done

echo "3. 编译库文件..."
for file in ../cbp2make/lib/*.cpp; do
    if [ -f "$file" ]; then
        base=$(basename "$file" .cpp)
        compile_file "$file" "$OBJDIR/$base.o" || exit 1
    fi
done

echo "4. 编译 TinyXML 库文件..."
for file in ../cbp2make/lib/tinyxml/*.cpp; do
    if [ -f "$file" ]; then
        base=$(basename "$file" .cpp)
        compile_file "$file" "$OBJDIR/$base.o" || exit 1
    fi
done

echo "5. 链接可执行文件..."
$CXX $LDFLAGS $OBJDIR/*.o -o $TARGET

if [ $? -eq 0 ]; then
    echo "✅ 构建成功: $TARGET"
    echo "文件大小: $(du -h $TARGET | cut -f1)"
else
    echo "❌ 链接失败"
    exit 1
fi
